// pages/scan/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

    // 扫码
    toScan: function (e) {
        var that = this;
        var type = e.currentTarget.dataset.type;
        wx.scanCode({
            success: function (res) {
                console.log(res.result);
                var code = that.getParamByUrl({url: res.result, key: 'code'});
                console.log(code);
                if (type == 1) {
                    that.toScene(code); return
                } else if (type == 2) {
                    that.toOne(code); return
                } else {
                    wx.showToast({
                        title: 'error', icon: 'none'
                    }); return
                }
            }
        });
    },

    // 去场景列表
    toScene: function (e) {
        wx.request({
          method: 'POST',
          url: app.globalData.domainXcx + '/scan/isExist',
          data: {code: e},
          timeout: 10000, //接口超时10S
          success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
              wx.showToast({
                title: res.msg, icon: 'none'
              }); return
            }
            wx.navigateTo({
                url: '/pages/scan/scene/index?code=' + e,
            }); return
          },
          fail: function () {
            console.log('error');
          }
        });
    },

    // 去详情
    toOne: function (e) {
        wx.request({
          method: 'POST',
          url: app.globalData.domainXcx + '/scan/typebycode',
          data: {code: e},
          timeout: 10000, //接口超时10S
          success: function (res) {
            res = res.data;
            console.log(res);
            if (res.code!=0) {
              wx.showToast({
                title: res.msg, icon: 'none'
              }); return
            }
            var type = res.data.type;
            var id = res.data.id;
            var viewType = '';
            if (type == 1) {
                viewType = 'tmp';
            } else if (type == 2) {
                viewType = 'shop';
            } else if (type == 3) {
                viewType = 'goods';
            } else if (type == 4) {
                viewType = 'user';
            } else {
              wx.showToast({
                title: '页面错误', icon: 'none'
              }); return
            }
            wx.navigateTo({
                url: '/pages/index/' + viewType + '/index?code=' + e + '&id=' + id,
            }); return
          },
          fail: function () {
            console.log('error');
          }
        });
    },

    // 编号查询
    toSerial: function (e) {
        wx.navigateTo({
            url: '/pages/scan/serial/index',
        });
    },

    // // 从URL中获取域名
    // getDomainByUrl: function (e) {
    //     var httpUrls = e.split('?');
    //     if (httpUrls[0] != app.globalData.domain) {
    //         wx.showToast({
    //             title: '非法二维码', icon: 'none'
    //         }); return
    //     }
    //     return httpUrls[0];
    // },

    // 从URL中获取参数
    getParamByUrl: function (e) {
        var httpUrls = e.url.split('?');
        console.log(app.globalData.domain);
        console.log(httpUrls);
        if (httpUrls[0] != app.globalData.domain 
            && httpUrls[0] != app.globalData.domainH5) {
            wx.showToast({
                title: '非法二维码', icon: 'none'
            }); return
        }
        if (httpUrls[1] == '') {
            wx.showToast({
                title: '没有参数', icon: 'none'
            }); return
        }
        // console.log(httpUrls[1]);
        var params = httpUrls[1].split('&');
        var i = 0, kvs = '', val = '';
        for (; i < params.length; i ++) {
            kvs = params[i].split('=');
            if (kvs[0] == e.key) {
                val = kvs[1];
            }
        }
        return val;
    },

    // 收纳储藏
    toShouna: function (e) {
        wx.navigateTo({
            url: '/pages/index/shounaAdd/index',
        });
    },
})